﻿using Microsoft.Crm.Sdk.Messages;
using Microsoft.Xrm.Sdk;
using Microsoft.Xrm.Sdk.Query;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace AccessTeamWorkflow.EntityHelpers
{
    public class AccessTeamHelper
    {
        public static readonly string EntityLogicalName = "teamtemplate";
        public static readonly string Field_TeamTemplateName = "teamtemplatename";

        private IOrganizationService _service;
        public AccessTeamHelper(IOrganizationService service)
        {
            _service = service;
        }

        /// <summary>
        /// Finds the access team by name.
        /// </summary>
        /// <param name="accessTeamName">Name of the access team.</param>
        /// <param name="_service">The _service.</param>
        /// <returns></returns>
        public EntityReference FindAccessTeamByName(String accessTeamName)
        {
            EntityReference result = null;

            //  Query using ConditionExpression and FilterExpression
            ConditionExpression condition = new ConditionExpression();
            condition.AttributeName = Field_TeamTemplateName;
            condition.Operator = ConditionOperator.Equal;
            condition.Values.Add(accessTeamName);

            // filter creation 
            FilterExpression filter = new FilterExpression();
            filter.Conditions.Add(condition);

            //create query expression 
            QueryExpression query = new QueryExpression(EntityLogicalName);
            query.Criteria.AddFilter(filter);
            query.ColumnSet = new ColumnSet(true);

            // execute query which will retrieve the Access team teamplate 
            EntityCollection queryResult = _service.RetrieveMultiple(query);
            if (queryResult != null && queryResult.Entities.Any())
            {
                result = queryResult.Entities.FirstOrDefault().ToEntityReference();
            }

            return result;
        }

        /// <summary>
        /// Adds the user to access team.
        /// </summary>
        /// <param name="userId">The user identifier.</param>
        /// <param name="accessTeamId">The access team identifier.</param>
        /// <param name="record">The record.</param>
        /// <param name="_service">The CRM _service.</param>
        public void AddUserToAccessTeam(Guid userId, Guid accessTeamId, EntityReference record)
        {
            var addRequest = new AddUserToRecordTeamRequest
            {
                SystemUserId = userId,
                TeamTemplateId = accessTeamId,
                Record = record
            };

            // execute the add request
            _service.Execute(addRequest);
        }

        /// <summary>
        /// Removes the user from access team.
        /// </summary>
        /// <param name="userId">The user identifier.</param>
        /// <param name="accessTeamId">The access team identifier.</param>
        /// <param name="record">The record.</param>
        /// <param name="_service">The CRM _service.</param>
        public void RemoveUserFromAccessTeam(Guid userId, Guid accessTeamId, EntityReference record)
        {
            var removeRequest = new RemoveUserFromRecordTeamRequest
            {
                SystemUserId = userId,
                TeamTemplateId = accessTeamId,
                Record = record
            };

            // execute the remove request
            _service.Execute(removeRequest);
        }
    }
}
